package com.dtsz.m2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.TaskCount;
import com.dtsz.m2.ui.view.progress.WaterWaveProgress;
import com.dtsz.m2.utils.StringUtils;
import com.dtsz.m2.utils.TaskCountdownUtils;
import com.dtsz.m2.volley.state.HUCState;

public class QuotaActivity extends BaseActivity {

    private WaterWaveProgress waveProgress;
    private TextView tvTask,tvContent;
    private LinearLayout llTask;
    private TaskCount taskCount = new TaskCount();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_quota);
        super.onCreate(savedInstanceState);
        waveProgress = findViewById(R.id.waterWaveProgress1);
        waveProgress.setShowProgress(true);
        waveProgress.animateWave();
        waveProgress.setmShowText(false);
        tvActionbarTitle.setText("抢名额");
        getTaskCount();
    }

    @Override
    public void initView() {
        super.initView();
        tvTask = findViewById(R.id.tv_task);
        llTask = findViewById(R.id.ll_task);
        tvContent = findViewById(R.id.tv_content);
    }

    @Override
    public void initClick() {
        super.initClick();
        tvTask.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_task:
                getAddCount();
                break;
        }
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (response instanceof TaskCount) {
                    TaskCount taskCount = (TaskCount) response;
                    int clickNum = Integer.parseInt(taskCount.getClickNum());
                    int num = Integer.parseInt(taskCount.getNum());
                    waveProgress.setMaxProgress(clickNum);
                    int currentProgress = clickNum - num;
                    waveProgress.setProgress(currentProgress);
                    tvContent.setText(taskCount.getRemark());
                    if (currentProgress > 0) {
                        tvTask.setEnabled(true);
                        tvTask.setText("抢名额");
                        llTask.setBackgroundResource(R.drawable.radius42_down);
                    } else {
                        tvTask.setText("今天名额发放完毕");
                        tvTask.setEnabled(false);
                        llTask.setBackgroundResource(R.drawable.radius42_down_h);
                    }
                }
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                String str = response.toString();
                if ("实名认证已授权".equals(str)) {
                    finish();
                    sendIntent(this, TaskAttestationActivity.class);
                } else {
                    getTaskCount();
                    showToast(str);
                }
                break;
        }
        loadDismiss();
    }

    private void getTaskCount() {
        //获取网络数据
        loading();
        pageVo.setType("名额领取");
        hucRequestInterfase.executeStr(this, TaskCount.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.TASKONEATTRES_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }

    public void getAddCount() {
        loading();
        taskCount.setToaType("名额领取");
        taskCount.setToken(pageVo.getToken());
        hucRequestInterfase.executeStr(this, String.class,
                HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.ADDTASKATTRE_URL,
                hucParameInterfase.getRequestParameter(taskCount), true);
    }

}
